package com.oocl.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oocl.dao.MerchantsDao;
import com.oocl.dao.base.BaseJPADaoUtil;
import com.oocl.entity.Merchants;


@Repository("merchantsDao")
public class MerchantsDaoImpl extends BaseJPADaoUtil implements MerchantsDao{

	
	/*
     * @Method  loadMerchant
     * @function load a Merchant for login
     * @param merchant Merchants
     * @return m Merchants
     * @author garen
     * @date 2017-8-12
     * */
	public Merchants loadMerchant(Merchants merchant) {
		String sql = "select m from Merchants m where m.mname=:mname and m.mpwd=:mpwd";
		Merchants m = (Merchants) entityManager.createQuery(sql)
		                       .setParameter("mname", merchant.getMname())
		                       .setParameter("mpwd", merchant.getMpwd())
		                       .getResultList().get(0);
		return m;
		
	}

	
	/*
     * @Method  findAliveMerchants
     * @function load Merchants where status1 != 2 and status2 = 2
     * @param 
     * @return list : Merchants List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Merchants> findAliveMerchants() {
		String sql = "select m from Merchants m where m.status1 !=2 and m.status2=2 and m.operatStatus=1";
		List<Merchants> list= entityManager.createQuery(sql).getResultList();
		return list;
	}
  
	
	/*
     * @Method  loadMerchant
     * @function load a Merchant by icnum
     * @param icnum : Merchants idcard number
     * @return m Merchants
     * @author garen
     * @date 2017-8-12
     * */
	public Merchants findmerchantsByIcnum(String icnum) {
		String sql = "select m from Merchants m where m.icnum=:icnum";
		Merchants m = (Merchants) entityManager.createQuery(sql)
		                       .setParameter("icnum", icnum)
		                       .getResultList().get(0);
		return m;
	}


	@Override
	public List<Merchants> findmerchantsByMname(String mname) {
		String sql = "select m from Merchants m where m.mname like '%"+mname+"%'";
		List<Merchants> mList = entityManager.createQuery(sql)
		                       .getResultList();
		return mList;
	}
   
}
